/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import MessageVariable.ClientSign;
import MessageVariable.ServerSign;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
import sun.misc.BASE64Encoder;
import ticketbooking.ClientKey;
import ticketbooking.Crypto;

/**
 *
 * @author LEHOANGKHANH
 */
public class MainFrameUser extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    String serverIP = null;//lay tu userloginframe
    int serverSessionPort;//lay tu userloginframe
    ClientKey clientKey = null;//lay tu userloginframe
    ClientSign clientSign = null;//khoi tao sau
    ServerSign serverSign = null;//lay tu userloginframe
    SecretKey sessionKey = null;//lay tu userloginframe
    String userName = null;//lay tu userloginframe
    String passWord = null;//lay tu userloginframe
    Socket socket = null;//lay tu userloginframe
    ObjectOutputStream out = null;//lay tu userloginframe
    ObjectInputStream in = null;//lay tu userloginframe
    
    UserLogin root = null;
    public MainFrameUser(UserLogin rootFrame) {
        this.root = rootFrame;
        this.serverIP = rootFrame.getServerIP();
        this.serverSessionPort = rootFrame.getServerSign().getPort();
        this.clientKey = rootFrame.clientKey;
        this.serverSign = rootFrame.getServerSign();
        this.sessionKey = rootFrame.getSessionKey();
        this.userName = rootFrame.getUserName();
        this.passWord = rootFrame.getPassWord();
        this.socket = rootFrame.getSocket();
        this.out = rootFrame.getOutStream();
        this.in = rootFrame.getInStream();
        //tao lai client sign
        clientSign = new ClientSign();
        clientSign.setIP(socket.getLocalAddress().getHostAddress());
        clientSign.setPort(socket.getLocalPort());
        clientSign.setUserName(userName);
        clientSign.setPass(passWord);
        clientSign.setPublicKeyRSA(clientKey.getPublicKeyRSA());
        clientSign.setPublicKeySign(clientKey.getPubliKeySign());
        clientSign.setSessionKey(sessionKey);
        initComponents();
        this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width,java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-45);
    }
    public ClientSign getClientSign(){
        return this.clientSign;
    }
    public ServerSign getServerSign(){
        return this.serverSign;
    }
    public String getServerIP(){
        return this.serverIP;
    }
    public int getServerSessionPort(){
        return this.serverSessionPort;
    }
    public String getUserName(){
        return this.userName;
    }
    public String getPassWord(){
        return this.passWord;
    }
    public Socket getSocket(){
        return this.socket;
    }
    public ClientKey getClientKey(){
        return this.clientKey;
    }
    public SecretKey getSessionKey(){
        return this.sessionKey;
    }
    public ObjectOutputStream getOutStream(){
        return this.out;
    }
    public ObjectInputStream getInStream(){
        return this.in;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem4 = new javax.swing.JMenuItem();
        DesktopPane = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu7 = new javax.swing.JMenu();
        jMenu1 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();

        jMenuItem4.setText("jMenuItem4");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Booking Ticket");
        setAutoRequestFocus(false);
        setForeground(java.awt.Color.white);
        setName("Book Ticket"); // NOI18N

        DesktopPane.setForeground(new java.awt.Color(153, 153, 153));

        javax.swing.GroupLayout DesktopPaneLayout = new javax.swing.GroupLayout(DesktopPane);
        DesktopPane.setLayout(DesktopPaneLayout);
        DesktopPaneLayout.setHorizontalGroup(
            DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1148, Short.MAX_VALUE)
        );
        DesktopPaneLayout.setVerticalGroup(
            DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 478, Short.MAX_VALUE)
        );

        jMenu3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/search.png"))); // NOI18N
        jMenu3.setText("Tìm Kiếm      ");
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });

        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/date.png"))); // NOI18N
        jMenuItem1.setText("Tìm Theo Ngày");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/station.png"))); // NOI18N
        jMenuItem2.setText("Tìm Theo Ga");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/datesation.png"))); // NOI18N
        jMenuItem3.setText("Tìm Theo Ngày & Ga");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuBar1.add(jMenu3);

        jMenu4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/change.png"))); // NOI18N
        jMenu4.setText("Thay Đổi Thông Tin");
        jMenu4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu4MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu4);

        jMenu2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/booked.png"))); // NOI18N
        jMenu2.setText("Đặt Vé               ");
        jMenu2.setEnabled(false);
        jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu2MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu2);

        jMenu7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/dsdk.png"))); // NOI18N
        jMenu7.setText("Danh Sách Vé Đặt");
        jMenu7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu7MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu7);

        jMenu1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/del.png"))); // NOI18N
        jMenu1.setText("Hủy Vé             ");
        jMenu1.setEnabled(false);
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu1);

        jMenu6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/cert.png"))); // NOI18N
        jMenu6.setText("Certificate    ");

        jMenuItem5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/export.png"))); // NOI18N
        jMenuItem5.setText("Export PublicKey RSA");
        jMenu6.add(jMenuItem5);

        jMenuItem6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/export.png"))); // NOI18N
        jMenuItem6.setText("Export PrivateKey RSA");
        jMenu6.add(jMenuItem6);

        jMenuItem8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/export.png"))); // NOI18N
        jMenuItem8.setText("Export PublicKey Sign");
        jMenu6.add(jMenuItem8);

        jMenuItem7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/export.png"))); // NOI18N
        jMenuItem7.setText("Export PrivateKey Sign");
        jMenu6.add(jMenuItem7);

        jMenuBar1.add(jMenu6);

        jMenu5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/help.png"))); // NOI18N
        jMenu5.setText("Trợ Giúp");
        jMenu5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu5MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu5);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(DesktopPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(DesktopPane)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        ChonNgay chonngay = new ChonNgay(this);
        DesktopPane.add(chonngay);
        chonngay.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        ChonGa chonga = new ChonGa(this);
        DesktopPane.add(chonga);
        chonga.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        ChonGa_Ngay gangay = new ChonGa_Ngay(this);
        DesktopPane.add(gangay);
        gangay.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenu4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu4MouseClicked
        // TODO add your handling code here:
        ThayDoiThongTin tt = new ThayDoiThongTin(this);
        DesktopPane.add(tt);
        tt.setVisible(true);
    }//GEN-LAST:event_jMenu4MouseClicked

    private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu2MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu2MouseClicked

    private void jMenu7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu7MouseClicked
        // TODO add your handling code here:
        QLDSVeDat ds = new QLDSVeDat(this);
        DesktopPane.add(ds);
        ds.setVisible(true);
    }//GEN-LAST:event_jMenu7MouseClicked

    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu1MouseClicked

    private void jMenu5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu5MouseClicked
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jMenu5MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrameUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrameUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrameUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrameUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /*KeyPair kp = Crypto.KeyPairGenerator();
        PublicKey publicKey = kp.getPublic();
        PrivateKey privateKey = kp.getPrivate();
        SecretKey sessionKey = Crypto.sessionKeyGenerator();
        System.out.println(sessionKey);
        
        SealedObject SO = Crypto.encryptObjectWithRSA(sessionKey, publicKey);
        Object o = Crypto.decryptObjectWithRSA(SO, privateKey);
        
        if (o instanceof SecretKey) {
                System.out.println("TRUE");
                SecretKey decryptedKey = (SecretKey) o;
                System.out.println(decryptedKey);
        } else {
                System.out.println("FALSE");
        }*/
        
        SecretKey sessionKey = Crypto.sessionKeyGenerator();
        System.out.println(new BASE64Encoder().encode(Crypto.IV(sessionKey)));
        

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new MainFrameUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane DesktopPane;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    // End of variables declaration//GEN-END:variables
}
